import api from './index';
import { dashboardOverview, alertDistribution, recentActivities, deviceUsage, getUseMockData } from './mockData.fixed';

// 获取仪表板数据
export const getDashboardData = async () => {
  // 检查是否使用模拟数据
  if (getUseMockData()) {    console.log('[API调用] GET /dashboard - 获取仪表盘数据');
    return { data: dashboardOverview };
  }

  try {
    return await api.get('/dashboard');
  } catch (error) {
    console.error('获取仪表板数据失败', error);
    return { data: dashboardOverview };
  }
};

// 获取最新的空气质量数据
export const getLatestAirData = async () => {
  // 检查是否使用模拟数据
  if (getUseMockData()) {
    // 简单的空气质量数据模拟
    const mockAirData = {
      pm25: 35,
      pm10: 65,
      co2: 800,
      tvoc: 0.4,
      temperature: 24.5,
      humidity: 45,
      updateTime: new Date().toISOString(),
      aqi: 75,
      aqiLevel: 'moderate'
    };
    return { data: mockAirData };
  }

  try {
    return await api.get('/dashboard/latestData');
  } catch (error) {
    console.error('获取最新空气质量数据失败', error);
    const mockAirData = {
      pm25: 35,
      pm10: 65,
      co2: 800,
      tvoc: 0.4,
      temperature: 24.5,
      humidity: 45,
      updateTime: new Date().toISOString(),
      aqi: 75,
      aqiLevel: 'moderate'
    };
    return { data: mockAirData };
  }
};

// 获取设备统计数据
export const getDeviceStats = async () => {
  // 检查是否使用模拟数据
  if (getUseMockData()) {
    return { data: deviceUsage };
  }

  try {
    return await api.get('/dashboard/deviceStats');  } catch (error) {
    console.error('获取设备统计数据失败', error);
    return { data: deviceUsage };
  }
};

// 获取空气质量统计数据
export const getAirQualityStats = async () => {
  // 检查是否使用模拟数据
  if (getUseMockData()) {
    // 简单的空气质量统计数据模拟
    const mockStats = {
      goodHours: 18,
      moderateHours: 5,
      poorHours: 1,
      dailyAverage: {
        pm25: 42,
        pm10: 68,
        co2: 750,
        tvoc: 0.35
      }
    };
    return { data: mockStats };
  }

  try {
    return await api.get('/dashboard/airQualityStats');  } catch (error) {
    console.error('获取空气质量统计数据失败', error);
    const mockStats = {
      goodHours: 18,
      moderateHours: 5,
      poorHours: 1,
      dailyAverage: {
        pm25: 42,
        pm10: 68,
        co2: 750,
        tvoc: 0.35
      }
    };
    return { data: mockStats };
  }
};

// 获取趋势数据
export const getTrendData = async () => {
  // 检查是否使用模拟数据
  if (getUseMockData()) {
    // 简单的趋势数据模拟
    const mockTrends = {
      timePoints: ["00:00", "04:00", "08:00", "12:00", "16:00", "20:00"],
      pm25: [32, 28, 35, 42, 38, 30],
      pm10: [60, 55, 65, 70, 68, 62],
      co2: [720, 680, 750, 820, 790, 730],
      tvoc: [0.3, 0.28, 0.38, 0.45, 0.4, 0.32]
    };
    return { data: mockTrends };
  }

  try {
    return await api.get('/dashboard/trends');  } catch (error) {
    console.error('获取趋势数据失败，', error);
    const mockTrends = {
      timePoints: ["00:00", "04:00", "08:00", "12:00", "16:00", "20:00"],
      pm25: [32, 28, 35, 42, 38, 30],
      pm10: [60, 55, 65, 70, 68, 62],
      co2: [720, 680, 750, 820, 790, 730],
      tvoc: [0.3, 0.28, 0.38, 0.45, 0.4, 0.32]
    };
    return { data: mockTrends };
  }
};

// 获取仪表盘概览数据
export const getDashboardOverview = async () => {  // 检查是否使用模拟数据
  if (getUseMockData()) {
    return { data: dashboardOverview };
  }

  try {
    return await api.get('/dashboard/overview');  } catch (error) {
    console.error('获取仪表盘概览数据失败', error);
    return { data: dashboardOverview };
  }
};

// 获取最近活动数据
export const getRecentActivities = async () => {  // 检查是否使用模拟数据
  if (getUseMockData()) {
    return { data: recentActivities };
  }

  try {
    return await api.get('/dashboard/recent-activities');  } catch (error) {
    console.error('获取最近活动数据失败', error);
    return { data: recentActivities };
  }
};

// 获取告警分布数据
export const getAlertDistribution = async () => {  // 检查是否使用模拟数据
  if (getUseMockData()) {
    return { data: alertDistribution };
  }

  try {
    return await api.get('/dashboard/alertDistribution');  } catch (error) {
    console.error('获取告警分布数据失败', error);
    return { data: alertDistribution };
  }
};

// 更新告警统计数据
// 这个函数可以在需要时更新仪表盘中与告警相关的统计信息
export const updateAlarmStats = async () => {
  // 检查是否使用模拟数据
  if (getUseMockData()) {
    // 从alarmsMockData更新dashboardOverview中的数据
    try {
      const { alarmsMockData, dashboardOverview, updateAlertDistribution } = await import('./mockData.fixed');
      
      // 更新待处理告警数量
      const pendingAlarms = alarmsMockData?.data?.filter(a => a.status === 'pending')?.length || 0;
      dashboardOverview.pendingAlarms = pendingAlarms;
      
      // 更新总告警数量
      dashboardOverview.alarmCount = alarmsMockData?.data?.length || 0;
      
      // 更新今日告警数量（假设当天的日期是2025-05-29）
      const today = '2025-05-29'; // 可根据实际情况获取当前日期
      dashboardOverview.todayAlarmCount = 
        alarmsMockData?.data?.filter(a => a.alarmTime.startsWith(today))?.length || 0;
      
      // 更新告警分布数据
      updateAlertDistribution();
      
      return { success: true };
    } catch (error) {
      console.error('更新告警统计数据失败:', error);
      return { success: false };
    }
  }

  try {
    // 实际API调用，如果有这个端点的话
    return await api.post('/dashboard/updateAlarmStats');
  } catch (error) {
    console.error('更新告警统计数据失败:', error);
    return { success: false };
  }
};
